using System;
using L=Science.Physics.GeneralPhysics;
using M=Science.Mathematics.Calculus;

namespace Serway.Chapter08
{
	/// <summary>
	/// Example11: Force and Energy on an Atomic Scale
	/// The potential energy associated with the force between 
	/// two neutral atoms in a molecule can be modeled by the 
	/// Lennard-Jones potential energy function,
	/// where x is the separation of the atoms. 
	/// The function U(x) contains two parameters \sigma and \epsilon
	/// that determined from experiments. Sample values for the 
	/// interaction between two atoms in a molecule are 
	/// \sigma = 0.263 nm and \epsilon = 1.51 \times 10^{-22} J. 
	/// (A) Using a spreadsheet or similar tool. Graph this 
	/// function and find the most likely distance between the 
	/// two atoms.
	/// x = 2.95 \times 10^{-10} m
	/// (B) Determine the force that one atom exerts on the 
	/// other in the molecule as a function of separation and argue 
	/// that the way this force behaves is physically plausible 
	/// when the atoms are close together and far apart.
	/// F_x = 4\epsilon(12\sigma^12/x^13 - 6\sigma^6/x^7)
	/// </summary>
	public class Example11
	{
		public Example11()
		{
		}
		private string result;
		public string Result
		{
			get{return result;}
		}
		private double sigma = 0.263; //*Math.Pow(10.0,-9.0);
		private double epsilon = 1.51;  //*Math.Pow(10.0,-22.0);
		public void Compute()
		{
            Science.Mathematics.Function.DoubleToDouble f =
                new Science.Mathematics.Function.DoubleToDouble(fff);
			M.NonlinearSingleVariableFunction min 
                = new M.NonlinearSingleVariableFunction(f);
			min.FindMinimum(-100,100);
			result+=Convert.ToString(min.MinimumAt)+"nm";
			result+="\r\n"+Convert.ToString(min.MinimumValue)+"X10^(-22) J";
			//(B)
            L.Scalar.FunctionOfPosition ff = new L.Scalar.FunctionOfPosition(func);
			L.PotentialEnergy U = new L.PotentialEnergy();
            U.ScalarFunctionOfPosition = ff;
			L.Position x = new L.Position();
			x.X = 0.295207474574121; // nm
			x.Y = 0.0; // nm
			x.Z = 0.0; // nm
			L.Force F = new L.Force(U,x);
			result+="\r\n"+Convert.ToString(F.X)+"X10^(-22) N";
			result+="\r\n"+Convert.ToString
				(4.0*epsilon*(12.0*Math.Pow(sigma,12.0)/Math.Pow(x.X,13.0)
				 - 6.0*Math.Pow(sigma,6.0)/Math.Pow(x.X,7.0)))+"X10^(-22) N";

		}
        private double fff(double x)
        {
            L.Position p = new L.Position();
            p.X = x;
            L.Scalar s = func(p);
            return s.Magnitude;
        }
		private L.Scalar func(L.Position x)
	    {
            L.Scalar s = new L.Scalar();
			s.Magnitude = 4.0*epsilon*(Math.Pow(sigma/x.X,12.0)
				-Math.Pow(sigma/x.X,6.0));
            return s;
	    }		
	}
}
